package andy.ham;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by winner on 2018/6/19.
 */

public class myView extends Activity {
    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private NoteDao noteDao;
    private Note note;
    private Cursor cursor;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view);

        Button edit=(Button)findViewById(R.id.edit);
        Button delete=(Button)findViewById(R.id.delete);
        Button cancel=(Button)findViewById(R.id.view_cancel);
        TextView title=(TextView)findViewById(R.id.view_title);
        TextView body=(TextView)findViewById(R.id.view_body);

        initDAO();

        final Intent intent = getIntent();
        final Bundle bundle = new Bundle(this.getIntent().getExtras());
        final Long id = bundle.getLong("id");
        note = noteDao.loadByRowId(id);
        title.setText(note.getTitle());
        body.setText(note.getBody());

        edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String mid = Long.toString(id);
                Log.d("id", mid);
                Intent intent = new Intent(myView.this, DiaryEditor.class);
                intent.setAction(DiaryEditor.EDIT_DIARY_ACTION);
                Bundle bundle = new Bundle();
                bundle.putLong("id", id);
                intent.putExtras(bundle);
                Log.d("id", mid);
                startActivity(intent);
                myView.this.finish();
                cursor.requery();
            }
        });

        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder b = new AlertDialog.Builder(myView.this);
                b.setTitle("删除全部");
                b.setMessage("确认删除全部吗？");
                b.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Long id = bundle.getLong("id");
                        noteDao.deleteByKey(id);
                        Toast.makeText(myView.this, R.string.diary_delete_success,Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(myView.this, LifeDiary.class);
                        startActivity(intent);
                        myView.this.finish();
                    }
                });
                b.setNegativeButton("取消",null);
                b.create();
                b.show();
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(myView.this, LifeDiary.class);
                startActivity(intent);
                myView.this.finish();
            }
        });
    }

    private void initDAO() {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "notes-db",null);
        db = helper.getWritableDatabase();
        daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
        noteDao = daoSession.getNoteDao();
        cursor = db.query(noteDao.getTablename(),noteDao.getAllColumns(),null,null,null,null,null);
    }

}
